<?php
namespace app\super\controller;
use think\Db;
use app\super\validate\KnowledgeValidate;
class KnowledgeController extends CommonController {


    /**
     * 获取知识库列表
     */
    public function getInfo(){
        $where = '';
        $rows = $_POST['rows'];
        $page = $_POST['page'];
        if($_POST['keyword']!=''){
            $keyword = $_POST['keyword'];
            $where['a.knowledge_name|u.user_username|ut.team_name|ut.team_number'] = array('like',"%$keyword%");
        }

        if($_POST['knowledge_type']!=''){//状态
            $where['a.knowledge_type'] = $_POST['knowledge_type'];
        }
        if($_POST['start']!=''){//时间
            $where['a.knowledge_time'] =array('>=',$_POST['start']);
        }elseif ($_POST['end']!=''){
            $where['a.knowledge_time'] =array('<=',$_POST['end']);

        }elseif ($_POST['start']!=''&&$_POST['end']!=''){
            $where['a.knowledge_time'] =array('between',array($_POST['start'],$_POST['end']));

        }
//        if($_POST['end']!=''){//时间
//            $where['a.knowledge_time'] =array('<=',$_POST['end']);
//        }

        $res = Db::table('up_knowledge')->alias('a')
            ->join('up_u_user u','u.user_id = a.knowledge_publisher','left')
            ->join('up_team ut','ut.team_id = a.knowledge_team_id','left')
            ->join('up_u_admin uua','uua.user_id =a.knowledge_publisher','left')
            ->where($where)
            ->limit($rows * ($page - 1), $rows)
            ->field('u.user_username,a.*,ut.team_name,ut.team_number,uua.user_username as username')
            ->order('a.knowledge_time DESC')
            ->select();
        $total =Db::table('up_knowledge')->alias('a')
            ->join('up_u_user u','u.user_id = a.knowledge_publisher','left')
            ->join('up_team ut','ut.team_id = a.knowledge_team_id','left')
            ->join('up_u_admin uua','uua.user_id =a.knowledge_publisher','left')
            ->where($where)
            ->count();

        $data['total'] = $total;
        $data['rows'] = $res;
        return json_encode($data);
    }

    /**
     * 发布
     */
    public function Confirmation(){
        $cid = $_POST['cid'];
        if($cid!=''){
            $where['knowledge_id'] = $cid;
            $data['knowledge_status'] = 1;//审核通过
            $res =  Db::table('up_knowledge')->where($where)->update($data);
            if($res){
                $returnData['status'] = 1;
                $returnData['msg'] = "发布成功";
                return json_encode($returnData);
            }else{
                $returnData['status'] = 0;
                $returnData['msg'] = "发布失败";
                return json_encode($returnData);
            }
        }else{
            $returnData['status'] = 0;
            $returnData['msg'] = "传递参数错误";
            return json_encode($returnData);
        }
    }

    /***
     * 取消发布
     */
    public function QxConfirmation(){
        $cid = $_POST['cid'];
        if($cid!=''){
            $where['knowledge_id'] = $cid;
            $data['knowledge_status'] = 2;//审核通过
            $res =  Db::table('up_knowledge')->where($where)->update($data);
            if($res){
                $returnData['status'] = 1;
                $returnData['msg'] = "取消发布成功";
                return json_encode($returnData);
            }else{
                $returnData['status'] = 0;
                $returnData['msg'] = "取消发布失败";
                return json_encode($returnData);
            }
        }else{
            $returnData['status'] = 0;
            $returnData['msg'] = "传递参数错误";
            return json_encode($returnData);
        }
    }

    /**
     * 删除此课程
     */
    public function delConfirmation(){
        $cid = $_POST['cid'];
        if($cid!=''){
            $where['knowledge_id'] = $cid;
            $res =  Db::table('up_knowledge')->where($where)->delete();
            if($res){
                $returnData['status'] = 1;
                $returnData['msg'] = "删除成功";
                return json_encode($returnData);
            }else{
                $returnData['status'] = 0;
                $returnData['msg'] = "删除失败";
                return json_encode($returnData);
            }
        }else{
            $returnData['status'] = 0;
            $returnData['msg'] = "传递参数错误";
            return json_encode($returnData);
        }
    }

    /**
     * 添加页面
     */
    public function addInfo(){
        $res = array();
        $team_id = Db::table('up_team')->where('is_team',1)->column('team_id');
        $team= Db::table('up_team')->where('is_team',1)->field('team_id,team_name')->select();
        $this->assign('team',$team);

        if(count($team_id)>0){
            $where['team_id'] = array('IN',$team_id);
            $res = Db::table('up_team_members')->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.team_member_user_id')
                ->field('uuu.user_id,uuu.user_phone,uuu.user_username')
                ->where($where)
                ->select();
        }
        $this->assign('res',$res);
        return $this->fetch('knowledge/addInfo');
    }

    /***
     * 添加功能
     */
    public function insertKnowledge(){
        $validata = new KnowledgeValidate();
        if(!$validata->check($_POST)) {
            $returnData['status'] = 0;
            $returnData['msg'] = $validata->getError();
            return json_encode($returnData);
        }
        $uid = session('loginSession')['user_id'];
        if($_POST['knowledge_type']==2){//证明是图文咨询
            if($_FILES['file']['name']=='') {
                $returnData['status'] = 0;
                $returnData['msg'] = "上传文件格式不能为空";
                return json_encode($returnData);
            }
            $rand = date('YmdHis').rand(100000,999999);
            $exe = explode('.',$_FILES['file']['name']);
            $filePath = $_FILES['file']['tmp_name'];
            $info = $this->qnCommon(2,$rand,$exe[1],$filePath);
            if($info['status']!=200) {
                $returnData['status'] = 0;
                $returnData['msg'] = "上传文件格式不正确";
                return json_encode($returnData);
            }else{
                $data['knowledge_banner'] = QiNiu.$info['key'];;//图片存储路径
            }
        }

        $data['knowledge_id'] = $this->getRandChar(32);
        $data['knowledge_context'] = $_POST['knowledge_context'];
        $data['knowledge_name'] =  $_POST['knowledge_name'];
        $data['knowledge_status'] = 1;
        $data['knowledge_time'] =date('Y-m-d H:i:s');
        $data['knowledge_publisher'] = $uid;
        $data['knowledge_type'] = $_POST['knowledge_type'];
        $data['knowledge_mold'] = $_POST['knowledge_mold'];
        $res = Db::table('up_knowledge')->insert($data);
        if($res){
            $whereU['user_status'] = 1;
            $up_u_user =Db::table('up_u_user')->where($whereU)->column('user_id');
            if($_POST['knowledge_type']==2){
                $msg1 = "{\"msg\":\"20\"}";
                $msg='平台发布了新的图资讯，一睹为快！';
                $msg_data = array('msg'=>$msg,'code'=>20,'fid'=>$data['knowledge_id']);
                $code = 20;
                $code1 = 20;
                $this->sendJpush(2,$msg,$msg_data,$up_u_user,$msg1,$code,1,$code1);
            }elseif ($_POST['knowledge_type']==3){
                $msg1 = "{\"msg\":\"21\"}";
                $msg='平台发布了新的技术资料,快去看看！,一睹为快！';
                $msg_data = array('msg'=>$msg,'code'=>21,'fid'=>$data['knowledge_id']);
                $code = 21;
                $code1 = 21;

                $this->sendJpush(2,$msg,$msg_data,$up_u_user,$msg1,$code,1,$code1);
            }
            return json_encode(array("status" => 1, "msg" => "发布成功！"));
        }else{
            return json_encode(array("status" => 0, "msg" => "发布失败！"));
        }
    }

    /*
     * 查看详情
     */
    public function courseInfo(){
        $cid = $_POST['cid'];
        $res = Db::table('up_knowledge')->where('knowledge_id',$cid)->find();
        $this->assign('res',$res);
        return $this->fetch('knowledge/courseInfo');
    }

    /**
     * 编辑页面
     */
    public function updateKnowledge(){
        $cid = $_POST['nid'];
        $res = Db::table('up_knowledge')->where('knowledge_id',$cid)->find();
        $this->assign('res',$res);
        return $this->fetch('knowledge/updateKnowledge');

    }

    public function updateInfo(){
        $where['knowledge_id'] = $_POST['knowledge_id'];
        $validata = new KnowledgeValidate();
        if(!$validata->check($_POST)) {
            $returnData['status'] = 0;
            $returnData['msg'] = $validata->getError();
            return json_encode($returnData);
        }
        $result = Db::table('up_knowledge')->where($where)->find();
        if($result['knowledge_team_id']!=''){

            if($_POST['knowledge_type']!=3){

                if($_POST['knowledge_type']==1&&$_POST['knowledge_mold']==1){
                    $teamWhere['knowledge_team_id'] = $result['knowledge_team_id'];
                    $teamWhere['knowledge_type'] = 1;
                    $teamWhere['knowledge_id'] = array('NEQ',$_POST['knowledge_id']);
                    $result = Db::table('up_knowledge')->where($teamWhere)->field('knowledge_id')->find();
                    if($result['knowledge_id']!=''){
                        $returnData['status'] = 0;
                        $returnData['msg'] = "每个团队的Banner课程只能发布一个,你所在的团队已经发布了";
                        return json_encode($returnData);
                    }
                }

            if(!empty($_FILES['file']['type'])){
                $rand = date('YmdHis').rand(100000,999999);
                $exe = explode('.',$_FILES['file']['name']);
                $filePath = $_FILES['file']['tmp_name'];
                $info = $this->qnCommon(2,$rand,$exe[1],$filePath);
                if($info['status']!=200) {
                    $returnData['status'] = 0;
                    $returnData['msg'] = "上传文件格式不正确";
                    return json_encode($returnData);
                }else{
                    $data['knowledge_banner'] = QiNiu.$info['key'];;//图片存储路径
                }
              }
            }else{
                if($result['knowledge_type']!=3){
                    $data['knowledge_banner'] ='';//图片存储路径
                }
            }
        }else{
            if($_POST['knowledge_type']==2){
                if($result['knowledge_type']==$_POST['knowledge_type']){//证明原来就是图文咨询
                    if(!empty($_FILES['file']['type'])){
                        $rand = date('YmdHis').rand(100000,999999);
                        $exe = explode('.',$_FILES['file']['name']);
                        $filePath = $_FILES['file']['tmp_name'];
                        $info = $this->qnCommon(2,$rand,$exe[1],$filePath);
                        if($info['status']!=200) {
                            $returnData['status'] = 0;
                            $returnData['msg'] = "上传文件格式不正确";
                            return json_encode($returnData);
                        }else{
                            $data['knowledge_banner'] = QiNiu.$info['key'];//图片存储路径
                        }
                    }
                }else{
                    if($_FILES['file']['name']=='') {
                        $returnData['status'] = 0;
                        $returnData['msg'] = "上传文件格式不能为空";
                        return json_encode($returnData);
                    }
                    $rand = date('YmdHis').rand(100000,999999);
                    $exe = explode('.',$_FILES['file']['name']);
                    $filePath = $_FILES['file']['tmp_name'];
                    $info = $this->qnCommon(2,$rand,$exe[1],$filePath);
                    if($info['status']!=200) {
                        $returnData['status'] = 0;
                        $returnData['msg'] = "上传文件格式不正确";
                        return json_encode($returnData);
                    }else{
                        $data['knowledge_banner'] = QiNiu.$info['key'];;//图片存储路径
                    }
                }
            }else{
                $data['knowledge_banner'] = '';//图片存储路径

            }
    }


        $data['knowledge_context'] = $_POST['knowledge_context'];
        $data['knowledge_name'] =  $_POST['knowledge_name'];
        $data['knowledge_type'] = $_POST['knowledge_type'];
        $data['knowledge_mold'] = $_POST['knowledge_mold'];
        $res = Db::table('up_knowledge')->where($where)->update($data);
        if($res){
            return json_encode(array("status" => 1, "msg" => "发布成功！"));
        }else{
            return json_encode(array("status" => 0, "msg" => "发布失败！"));
        }
    }
}